<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require('includes/application_top.php');
$models = $_POST['models'];
$admins = $_POST['admins'];
$inout = $_POST['inout'];
$date_begin = $_POST['date_begin'];
$date_end = $_POST['date_end'];
$type = $_POST['type'];
$records = $_POST['records_number'];
$page = $_POST['page'];
if(!isset($records))
    $records = 20;
if(!isset($page))
    $page = 1;

global $db;

//get models of product
$products_model = $db->Execute("SELECT p.products_model, pd.products_name 
                            FROM " . TABLE_PRODUCTS . " p
                                JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON p.products_id = pd.products_id 
                                        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                            WHERE products_model <> '' and products_status='1' order by pd.products_name asc");
//get list admins
$list_admins = $db->Execute("SELECT admin_id, admin_name FROM " . TABLE_ADMIN . " order by admin_name");

$sql_report = "select p.products_model, pd.products_name, s.movements_units, s.stock_after
                    , cast(s.movements_date as date) as movements_date, a.admin_name, s.movements_comment
               from stock_movements s                    
                   join " . TABLE_PRODUCTS . " p on s.products_id = p.products_id
                   join " . TABLE_PRODUCTS_DESCRIPTION . " pd on s.products_id = pd.products_id and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                   left join " . TABLE_ADMIN . " a on s.admin_id = a.admin_id
                where 1 = 1";
$sql_where = "";
if(isset($models) && !empty($models))
    $sql_where .= " and p.products_model='$models'";
if(isset($admins) && !empty($admins))
    $sql_where .= " and s.admin_id='$admins'";
if(isset($inout) && !empty($inout))
{
    if($inout === "-1")
        $sql_where .= " and s.movements_units < 0";
    else
        $sql_where .= " and s.movements_units > 0";
}
if(isset($date_begin) && !empty($date_begin))
    $sql_where .= " and cast(s.movements_date as date)>='$date_begin'";
if(isset($date_end) && !empty($date_end))
    $sql_where .= " and cast(s.movements_date as date)<='$date_end'";
if(isset($type) && !empty($type))
    $sql_where .= " and s.movements_type = '$type'";

$sql_order = " order by s.movements_date";

$sql_order .= " limit " . (int)$records*((int)$page-1) . "," . (int)$records*(int)$page;


$report = $db->Execute($sql_report . $sql_where . $sql_order);

$query_total = $db->Execute("select count(s.movements_id) 
                    from stock_movements s                         
                        join " . TABLE_PRODUCTS . " p on s.products_id = p.products_id
                        join " . TABLE_PRODUCTS_DESCRIPTION . " pd on s.products_id = pd.products_id and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                        left join " . TABLE_ADMIN . " a on s.admin_id = a.admin_id
                        where 1 = 1 " . $sql_where
                    );
$total = $query_total->RecordCount();
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
<script language="javascript"><!--
  var dateBegin = new ctlSpiffyCalendarBox("dateBegin", "stock_movements", "date_begin","btnDate1","<?php echo $date_begin; ?>",scBTNMODE_CUSTOMBLUE);
  var dateEnd = new ctlSpiffyCalendarBox("dateEnd", "stock_movements", "date_end","btnDate2","<?php echo $date_end; ?>",scBTNMODE_CUSTOMBLUE);  
//--></script>
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script type="text/javascript" src="/jquery-1.7.1.js"></script>
<script type="text/javascript">
  <!--
  function init()
  {
    cssjsmenu('navbar');
    if (document.getElementById)
    {
      var kill = document.getElementById('hoverJS');
      kill.disabled = true;
    }
  }
  // -->
</script>
</head>
<body onload="init()">
    <div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<form action="stock_movements.php" name="stock_movements" id="stock_movements" method="post">        
    <table style="margin-left:auto; margin-right:auto; margin-top: 20px; margin-bottom:0px; width: 80%">
        <tr>
            <td>Products</td>
            <td>Admin</td>
            <td>In/Out</td>
            <td>Date Begin</td>
            <td>Date End</td>
            <td>Type</td>
            <td>Result #</td>
            <td>Page #</td>
            <td></td>
        </tr>
        <tr>
            <td><select name="models" style="width: 250px">
                    <option value="">ALL</option>
                    <?php 
                while(!$products_model->EOF)
                {
                    if($models == $products_model->fields['products_model'])
                        echo "<option selected='selected' value='" . $products_model->fields['products_model'] . "'>" 
                                . $products_model->fields['products_name'] . "(" . $products_model->fields['products_model'] . ")" . "</option>";
                    else                                
                        echo "<option value='" . $products_model->fields['products_model'] . "'>" 
                            . $products_model->fields['products_name'] . "(" . $products_model->fields['products_model'] . ")" . "</option>";
                    $products_model->MoveNext();
                }
                    ?>
                </select></td>
            <td>
                <select name="admins">
                    <option value="">ALL</option>
                    <?php 
                while(!$list_admins->EOF)
                {
                    if($admins === $list_admins->fields['admin_id'])
                        echo "<option selected='selected' value='" . $list_admins->fields['admin_id'] . "'>" . $list_admins->fields['admin_name'] . "</option>";
                    else                                
                        echo "<option value='" . $list_admins->fields['admin_id'] . "'>" . $list_admins->fields['admin_name'] . "</option>";
                    $list_admins->MoveNext();
                }
                    ?>
                </select>
            </td>
            <td><select name="inout">
                    <option value="">ALL</option>
                    <option value="1">In</option>
                    <option value="-1">Out</option>
                </select></td>
            <td><script language="javascript">dateBegin.writeControl(); dateBegin.dateFormat="yyyy-MM-dd";</script></td>
            <td><script language="javascript">dateEnd.writeControl(); dateEnd.dateFormat="yyyy-MM-dd";</script></td>
            <td> 
                <select name="type">
                    <option value="">ALL</option>
                    <option value="update_products">update_products</option>
                    <option value="edit_orders">edit_orders</option>
                    <option value="front_office">front_office</option>
                    <option value="stock_with_attributes">stock_with_attributes</option>
                    <option value="excel_upload">excel_upload</option>                        
                </select>
            </td>
            <td>
                <select name="records_number" onchange="$('#page').val('1');$('#stock_movements').submit();">                        
                    <option value="20" <?php echo $records == 20 ? "selected" : "";?>>20</option>
                    <option value="30" <?php echo $records == 30 ? "selected" : "";?>>30</option>
                    <option value="50" <?php echo $records == 50 ? "selected" : "";?>>50</option>
                    <option value="100" <?php echo $records == 100 ? "selected" : "";?>>100</option>
                    <option value="200" <?php echo $records == 200 ? "selected" : "";?>>200</option>                        
                </select>
            </td>
            <td>
                <select name="page" id="page" onchange="">                        
            <?php
                $number_of_page = floor($total / $records) + 1;
                for($i = 1; $i <= $number_of_page; $i++)
                {
                    echo "<option value='$i' " . ($i == $page ? "selected" : "") . ">$i</option>";
                }
            ?>
                </select>
            </td>
            <td>
                <input type="submit" onclick="$('#page').val('1');" value="Submit" />
            </td>
        </tr>
    </table>
</form>

<table style="margin-left:auto; margin-right:auto; margin-top: 20px; margin-bottom:30px; width: 80%">
    <tr class="dataTableHeadingRow">
        <td align="center" class="dataTableHeadingContent">Product Model</td>
        <td align="center" class="dataTableHeadingContent">Product Name</td>
        <td align="center" class="dataTableHeadingContent">Units</td>
        <td align="center" class="dataTableHeadingContent">Stock After</td>
        <td align="center" class="dataTableHeadingContent">Date</td>
        <td align="center" class="dataTableHeadingContent">Admin</td>
        <td align="center" class="dataTableHeadingContent">Comments</td>        
    </tr>
<?php if ($report->RecordCount() > 0){
    while(!$report->EOF){?>
    <tr onmouseout="rowOutEffect(this)" onmouseover="rowOverEffect(this)" class="dataTableRow">
        <td class="dataTableContent"><?php echo $report->fields['products_model']; ?></td>
        <td class="dataTableContent"><?php echo $report->fields['products_name']; ?></td>
        <td class="dataTableContent" align="right"><?php echo $report->fields['movements_units']; ?></td>
        <td class="dataTableContent" align="right"><?php echo $report->fields['stock_after']; ?></td>
        <td align="center" class="dataTableContent"><?php echo $report->fields['movements_date']; ?></td>
        <td align="center" class="dataTableContent"><?php echo $report->fields['admin_name']; ?></td>
        <td class="dataTableContent"><?php echo $report->fields['movements_comment']; ?></td>        
    </tr>
<?php
        $report->MoveNext();
    }
}?>
</table>
<!-- body_eof //-->

<!-- footer //-->
<div class="footer-area">
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
</div>
<!-- footer_eof //-->
<br />
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>